﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;
using System.Text;

/// <summary>
/// Summary description for SystemCommon
/// </summary>

public class Popup
{
    /// <summary>
    /// show the pop up div
    /// </summary>
    /// <param name="panel">container the button</param>
    /// <param name="url"></param>
    public static void ShowPopup(UpdatePanel panel, string url)
    {
        ScriptManager.RegisterClientScriptBlock(panel, panel.GetType(), "ShowPopup", "ShowPopup('" + url + "')", true);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="panel"></param>
    /// <param name="page">request page</param>
    public static void ClosePopup(UpdatePanel panel,Page page)
    {
        string pageIndex = page.Request["PageIndex"];
        string pagerId=page.Request["PagerId"];
        string js = "{0}self.parent.tb_remove();";
        if (pageIndex != null && pagerId != null)
        {
            int index = Convert.ToInt32(pageIndex)+1;
            string temp = "window.parent.__doPostBack('" + pagerId + "','" + "Page$"+index.ToString() + "');";
            js = string.Format(js,temp);
        }
       ScriptManager.RegisterClientScriptBlock(panel, panel.GetType(), "ClosePopup", js, true);
    }
}

public class CommomMethod
{
    public static IList ListTypeForEnum(Type type)
    {
        
        ArrayList list = new ArrayList();
        foreach (int i in Enum.GetValues(type))
        {
            ListItem listitem = new ListItem(Enum.GetName(type, i), i.ToString());
            list.Add(listitem);
        }
        return list;
    }
    /// <summary>
    /// bind a enum to the dropdownlist
    /// </summary>
    /// <param name="ddl"></param>
    /// <param name="type"></param>
    public static void BindEnumForDropdwonList( DropDownList ddl, Type type)
    {

        ddl.Items.Clear();
        ddl.DataSource = CommomMethod.ListTypeForEnum(type);
        ddl.DataValueField = "value";
        ddl.DataTextField = "text";
        ddl.DataBind();            
    }
    public static string  SubString(string value,int length)
    {
        if (value!=null&&value.Length > length)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<a title=\"");
            builder.Append(value);
            builder.Append("\">");
            builder.Append(value.Substring(0, length));
            builder.Append("..");
            builder.Append("</a>");
            return builder.ToString();
        }
        else
            return value;
    }
}

public class ConfigurationInfo
{
    public static int PageSize
    {
        get 
        {
            try
            {
                return Convert.ToInt32(ConfigurationManager.AppSettings["PageSize"]);
            }
            catch
            {
                return 25;
            }
        }
    }
}

/// <summary>
/// system modules
/// </summary>
/// 
public enum SystemModules
{
    /// <summary>
    /// 
    /// </summary>
    UserManagement = 0,

    /// <summary>
    /// PhoneManagement
    /// </summary>
    PhoneManagement = 1,
    /// <summary>
    /// BatteryManagement 
    /// </summary>
    BatteryManagement = 2,
    /// <summary>
    /// SearchPhoneByBattery
    /// </summary>
    SearchPhoneByBattery = 3,
    /// <summary>
    /// SearchBatteryByPhone 
    /// </summary>
    SearchBatteryByPhone = 4,

}